
import 'dart:convert';

import 'package:base_flutter_project/model/card_model_item.dart';
import 'package:base_flutter_project/model/cardmodel.dart';
import 'package:base_flutter_project/model/model.dart';
import 'package:http/http.dart' as http;

const _API_URL = "https://www.devio.org/io/flutter_app/json/home_page.json";
const _API_CARD = "https://www.devio.org/io/flutter_app/json/travel_page.json";

var _params = {
    "districtId": -1,
    "groupChannelCode": "tourphoto_global1",
    "type": null,
    "lat": -180,
    "lon": -180,
    "locatedDistrictId": 2,
    "pagePara": {
      "pageIndex": 1,
      "pageSize": 10,
      "sortType": 9,
      "sortDirection": 0
    }
};
class Network {
  static Future<Autogenerated> fetch() async {
    final  response = await http.get(_API_URL);
    // 解决中文乱码
    if(response.statusCode == 200){
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return Autogenerated.fromJson(result);
    } else {
      throw Exception("Fail to load home_page.json");
    }
  }

  static Future<CardAutogenerated> card_fetch() async {
    final  response = await http.get(_API_CARD);
    // 解决中文乱码
    if(response.statusCode == 200){
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return CardAutogenerated.fromJson(result);
    } else {
      throw Exception("Fail to load home_page.json");
    }
  }

  static Future<CardItemAutogenerated> card_item_fetch(String url, String groupChannelCode, int pageIndex, int pageSize) async {
    Map paramsMap = _params['pagePara'];
    paramsMap['pageIndex'] = pageIndex;
    paramsMap['pageSize'] = pageSize;
    _params['groupChannelCode'] = groupChannelCode;
    final  response = await http.post(url,body: jsonEncode(_params));
    // 解决中文乱码
    if(response.statusCode == 200){
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      print(result);
      return CardItemAutogenerated.fromJson(result);
    } else {
      throw Exception("Fail to load home_page.json");
    }
  }
 }